using System;
using System.IO;
using System.Reflection;

namespace MsSqlSpatial.Utility
{
    public static class Command
    {
        private static readonly string[] orderedAssemblyFilenames = new[]
                                                                        {
                                                                            "GeoAPI.dll",
                                                                            "ProjNet.dll",
                                                                            "NetTopologySuite.dll",
                                                                            "MsSqlSpatialLibrary.dll"
                                                                        };

        public static void Deploy(ConnectionOptions connection, DeployPermission permission)
        {
            string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            string[] filenames = Array.ConvertAll(orderedAssemblyFilenames, fileName =>
                                                                            Path.Combine(baseDirectory, fileName)
                );

            var deployment = new Deployment();
            deployment.ProgressMessage += deployment_ProgressMessage;
            deployment.DeployAssemblies(filenames, connection.GetSqlServerConnectionString(), connection.Database,
                                        permission);
        }

        /// <summary>
        /// Removes assembly and associated objects from SQL Server database.
        /// </summary>
        /// <param name="general"></param>
        public static void Undeploy(ConnectionOptions connection)
        {
            string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            string[] filenames = Array.ConvertAll(orderedAssemblyFilenames, fileName =>
                                                                            Path.Combine(baseDirectory, fileName)
                );
            Array.Reverse(filenames);

            var deployment = new Deployment();
            deployment.ProgressMessage += deployment_ProgressMessage;
            deployment.UndeployAssemblies(filenames, connection.GetSqlServerConnectionString(), connection.Database);
        }


        private static void deployment_ProgressMessage(string message)
        {
            Console.WriteLine(message + "...");
        }

        /// <summary>
        /// Imports from Shapefile datasource to SQL Server.
        /// </summary>
        /// <param name="general"></param>
        /// <param name="options"></param>
        /// <returns>Task stats.</returns>
        public static TaskStats ImportShapefile(
            ConnectionOptions connection,
            SqlServerOptions options,
            string shapefilePath,
            bool appendRows)
        {
            TaskStats result;

            StoredProcedures.ImportFromShapefileMain(
                shapefilePath,
                options.Schema,
                options.Table,
                options.Column,
                options.SRID,
                options.Subtype,
                connection.GetSqlServerConnectionString(),
                appendRows,
                out result.RowsProcessed,
                out result.ElapsedTime);

            return result;
        }

        /// <summary>
        /// Imports from PostGIS datasource to SQL Server.
        /// </summary>
        /// <param name="general"></param>
        /// <param name="options"></param>
        /// <returns>Task stats.</returns>
        public static TaskStats ImportPostGis(
            ConnectionOptions sqlServerConnection,
            ConnectionOptions postGisConnection,
            SqlServerOptions sqlServerOptions,
            PostGisOptions postGisOptions,
            bool appendRows)
        {
            using (
                var import = new ImportPostGis(sqlServerConnection, postGisConnection, sqlServerOptions, postGisOptions,
                                               appendRows))
                return import.Execute();
        }
    }
}